package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.EventsBiz;
import com.egao.orderB.entity.Events;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SingleColumnRowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-05-11 16:40:01
 * @version ：1.0
 */
@Repository
public class EventsBizImpl extends BaseDao implements EventsBiz {

	@Override
	public JsonResult list(PageResult2<T> page, Events events, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Events> rm = new BeanPropertyRowMapper<Events>(Events.class);
		StringBuffer sql = new StringBuffer("select e.id,e.organization_id,e.customer_id");
        sql.append(",e.customer,e.bill_id,e.driver_id,e.driver,e.event_date");
		sql.append(",e.car_no,e.car_team_id,e.car_team,e.content");
		sql.append(",e.remark,e.type,e.create_time,e.create_user");
		sql.append(",e.update_time,e.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
        sql.append(" from chf_events e ");
        sql.append(" inner join sys_user suser on suser.user_id=e.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=e.organization_id ");
        sql.append(" where e.deleted=0 and sogzn.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and e.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(events)){
			if(Utils.isNotEmpty(events.getCustomerId())){
				sql.append(" and instr(e.customer_id,:customer_id)>0 ");
				map.put("customer_id", events.getCustomerId());
			}
			if(Utils.isNotEmpty(events.getCustomer())){
				sql.append(" and instr(e.customer,:customer)>0 ");
				map.put("customer", events.getCustomer());
			}
			if(Utils.isNotEmpty(events.getType())){
				sql.append(" and instr(e.type,:type)>0 ");
				map.put("type", events.getType());
			}
			if(Utils.isNotEmpty(events.getDate1())){
				sql.append(" and e.event_date>= :date1 ");
				map.put("date1", events.getDate1());
			}
			if(Utils.isNotEmpty(events.getDate2())){
				sql.append(" and e.event_date<=:date2 ");
				map.put("date2", events.getDate2());
			}
			if(Utils.isNotEmpty(events.getSort())&&Utils.isNotEmpty(events.getOrder())){
                sql.append(" order by e."+events.getSort()+" "+events.getOrder());
            } else {
                sql.append(" order by e.create_time desc");
            }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Events> list(Events events, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Events> rm = new BeanPropertyRowMapper<Events>(Events.class);
        StringBuffer sql = new StringBuffer("select e.id,e.organization_id,e.customer_id");
        sql.append(",e.customer,e.bill_id,e.driver_id,e.driver,e.event_date");
		sql.append(",e.car_no,e.car_team_id,e.car_team,e.content");
		sql.append(",e.remark,e.type,e.create_time,e.create_user");
		sql.append(",e.update_time,e.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
        sql.append(" from chf_events e ");
        sql.append(" inner join sys_user suser on suser.user_id=e.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=e.organization_id ");
        sql.append(" where e.deleted=0 and sogzn.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and e.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(events)){
			if(Utils.isNotEmpty(events.getCustomerId())){
				sql.append(" and instr(e.customer_id,:customer_id)>0 ");
				map.put("customer_id", events.getCustomerId());
			}
			if(Utils.isNotEmpty(events.getCustomer())){
				sql.append(" and instr(e.customer,:customer)>0 ");
				map.put("customer", events.getCustomer());
			}
			if(Utils.isNotEmpty(events.getType())){
				sql.append(" and instr(e.type,:type)>0 ");
				map.put("type", events.getType());
			}
			if(Utils.isNotEmpty(events.getDate1())){
				sql.append(" and e.event_date>= :date1 ");
				map.put("date1", events.getDate1());
			}
			if(Utils.isNotEmpty(events.getDate2())){
				sql.append(" and e.event_date<=:date2 ");
				map.put("date2", events.getDate2());
			}
            if(Utils.isNotEmpty(events.getSort())&&Utils.isNotEmpty(events.getOrder())){
				sql.append(" order by e."+events.getSort()+" "+events.getOrder());
			} else {
				sql.append(" order by e.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public List<Events> allCustomer(Events events, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Events> rm = new BeanPropertyRowMapper<Events>(Events.class);
        StringBuffer sql = new StringBuffer("");
//        sql.append(" select e2.customer_id,e2.customer, e2.enums-if(cen.nums is null, 0, cen.nums) messageNums ");
//        sql.append(" from (select e.customer_id, e.customer,count(1) enums ");
//        sql.append(" from (select e3.customer_id, e3.customer,e3.create_time  from chf_events e3 where e3.deleted=0  order by e3.create_time desc ) e ");
//		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
//            sql.append(" and e.organization_id=:tableOrganizationId ");
//            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
//        }
//		sql.append(" group by e.customer_id, e.customer ");
//		sql.append(" ) e2 ");
//		sql.append(" left join chf_events_num cen on cen.module=e2.customer_id and cen.user_id=:user_id ");
//		sql.append(" order by e2.customer_id desc ");
//		sql.append(" order by e2.enums-if(cen.nums is null, 0, cen.nums) desc ");
		sql.append("select e4.customer_id, e4.customer,e5.enums, e5.enums-if(cen.nums is null, 0, cen.nums) messageNums   from chf_events e4 ");
		sql.append("inner join (SELECT max(e6.id) id, count(1) enums  from chf_events e6 GROUP BY e6.customer_id ) e5 on e5.id=e4.id ");
		sql.append("left join chf_events_num cen on cen.module=e4.customer_id and cen.user_id=:user_id ");
		sql.append("where e4.deleted=0 order by e4.create_time  desc");
		map.put("user_id", loginUser.getUserId()+"");
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Integer getListNum(Events events) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Integer> rm = new SingleColumnRowMapper<Integer>(Integer.class);
		StringBuffer sql = new StringBuffer("select count(1) ");
		sql.append(" from chf_events ");
		sql.append(" where deleted=0 ");
		sql.append(" and customer_id=:customer_id ");
		map.put("customer_id", events.getCustomerId());
//		sql.append(" and DATE_FORMAT(pack_date,'%Y-%m-%d') =DATE_FORMAT(:pack_date,'%Y-%m-%d') ");
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(),map, rm);
	}

	@Override
	public Events getById(Events events) {
		RowMapper<Events> rm = new BeanPropertyRowMapper<Events>(Events.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select e.id,e.organization_id,e.customer_id");
        sql.append(",e.customer,e.bill_id,e.driver_id,e.driver,e.event_date");
		sql.append(",e.car_no,e.car_team_id,e.car_team,e.content");
		sql.append(",e.remark,e.type,e.create_time,e.create_user");
		sql.append(",e.update_time,e.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_events e ");
        sql.append(" inner join sys_user suser on suser.user_id=e.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=e.organization_id ");
        sql.append(" where e.deleted=0 and sogzn.deleted=0 and e.id=:id ");
		map.put("id", events.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(Events events, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_events (");
        sql.append("organization_id,customer_id,customer,bill_id");
		sql.append(",event_date,driver_id,driver,car_no,car_team_id");
		sql.append(",car_team,content,remark,type");
		sql.append(",create_user,create_time) values ( ");
        sql.append(":organization_id,:customer_id,:customer,:bill_id");
		sql.append(",:event_date,:driver_id,:driver,:car_no,:car_team_id");
		sql.append(",:car_team,:content,:remark,:type");
		sql.append(",:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("customer_id", events.getCustomerId());
        map.put("customer", events.getCustomer());
        map.put("bill_id", events.getBillId());
        map.put("event_date", events.getEventDate());
        map.put("driver_id", events.getDriverId()==null?null:events.getDriverId()+"");
        map.put("driver", events.getDriver());
        map.put("car_no", events.getCarNo());
        map.put("car_team_id", events.getCarTeamId()==null?null:events.getCarTeamId()+"");
        map.put("car_team", events.getCarTeam());
        map.put("content", events.getContent());
        map.put("remark", events.getRemark());
        map.put("type", events.getType());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(Events events, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_events set ");
        sql.append("customer_id=:customer_id,customer=:customer,bill_id=:bill_id ");
		sql.append(",event_date=:event_date ");
		sql.append(",driver_id=:driver_id,driver=:driver,car_no=:car_no ");
		sql.append(",car_team_id=:car_team_id,car_team=:car_team,content=:content ");
		sql.append(",remark=:remark,type=:type,update_time=now() ");
		sql.append(",update_user=:update_user where id=:id ");
        map.put("customer_id", events.getCustomerId());
        map.put("customer", events.getCustomer());
        map.put("bill_id", events.getBillId());
        map.put("event_date", events.getEventDate());
        map.put("driver_id", events.getDriverId()==null?null:events.getDriverId()+"");
        map.put("driver", events.getDriver());
        map.put("car_no", events.getCarNo());
        map.put("car_team_id", events.getCarTeamId()==null?null:events.getCarTeamId()+"");
        map.put("car_team", events.getCarTeam());
        map.put("content", events.getContent());
        map.put("remark", events.getRemark());
        map.put("type", events.getType());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", events.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(Events events, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(events.getId().length()>0){
			for(String ID : events.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_events set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
